package com.mcintyret.site.users;

import org.fest.assertions.GenericAssert;
import org.springframework.security.core.GrantedAuthority;

import java.util.Set;

/**
 * User: mcintyret2
 * Date: 09/04/2012
 */
public class UserAssert extends GenericAssert<UserAssert, User> {
    /**
     * Creates a new <code>{@link org.fest.assertions.GenericAssert}</code>.
     *
     * @param actual   the actual value to verify.
     */
    protected UserAssert(User actual) {
        super(UserAssert.class, actual);
    }

    public static UserAssert assertThat(User actual) {
        return new UserAssert(actual);
    }

    public UserAssert hasUsername(String expectedUsername) {
        String actualUsername = actual.getUsername();
        if (expectedUsername == null) {
            if (actualUsername == null) return this;
            throw failure("Expected null username but was " + actualUsername);
        } else {
            if (expectedUsername.equals(actualUsername)) return this;
            throw failure("Expected username to be " + expectedUsername + " but was " + actualUsername);
        }
    }

    public UserAssert hasPassword(String expectedPassword) {
        String actualPassword = actual.getPassword();
        if (expectedPassword == null) {
            if (actualPassword == null) return this;
            throw failure("Expected null password but was " + actualPassword);
        } else {
            if (expectedPassword.equals(actualPassword)) return this;
            throw failure("Expected password to be " + expectedPassword + " but was " + actualPassword);
        }
    }

    public UserAssert hasEmail(String expectedEmail) {
        String actualEmail = actual.getEmail();
        if (expectedEmail == null) {
            if (actualEmail == null) return this;
            throw failure("Expected null email but was " + actualEmail);
        } else {
            if (expectedEmail.equals(actualEmail)) return this;
            throw failure("Expected email to be " + expectedEmail + " but was " + actualEmail);
        }
    }

    public UserAssert hasAuthorities(Set<? extends GrantedAuthority> expectedAuthorities) {
        Set<? extends GrantedAuthority> actualAuthorities = actual.getAuthorities();
        if (expectedAuthorities == null) {
            if (actualAuthorities == null) return this;
            throw failure("Expected null authorities but was " + actualAuthorities);
        } else {
            if (expectedAuthorities.equals(actualAuthorities)) return this;
            throw failure("Expected authorities to be " + expectedAuthorities + " but was " + actualAuthorities);
        }
    }
}
